﻿using System;
using System.Collections.Generic;
using System.IO;
using ServerEngine;

namespace WorldOfMUDra
{
    public partial class Player : ServerClient
    {
        public void WhereAmICommand(List<string> message)
        {
            if (_loggedIn)
            {
                SendWorldMessage("You are in " + _location.Name + ". " + Location);
            }
            else
            {
                SendWorldMessage("You are in limbo. Please log in.");
            }
        }

        void LogInCommand(List<string> message)
        {
            if (message.Count >= 3)
            {
                string uname = message[1].Trim();
                string unhashedpass = message[2].Trim();
                PasswordStorageFile.CalculateMd5Hash(unhashedpass);
                if (Globals.PassFile.ContainsUser(uname))   // If the user has logged in before.
                {
                    SendWorldMessage("Welcome back " + uname + ". Please wait while we verify your credentials.");
                    if (Globals.PassFile.VerifyPasswordOf(uname, unhashedpass)) // If the user enters the correct password
                    {
                        _loggedIn = true;
                        _name = uname;
                        string configfilename = uname + ".xml";
                        SendWorldMessage("You have successfully logged in. Now sending you to your previous location.");
                        if (File.Exists(configfilename))
                        {
                            PlayerConfigFile pcf = new PlayerConfigFile(configfilename);
                            AddVisitedArea(pcf);
                            LoadAbilities(pcf);
                            World.Areas[pcf.LocationId].MoveHere(this);
                        }
                        else
                        {
                            Globals.Mudra.FirstArea.MoveHere(this);
                            PlaySound("tull", false);
                            Globals.CreateConfigFor(this);
                        }
                    }
                    else
                    {
                        SendWorldMessage("Sorry, but your credentials did not match.");
                    }

                }
                else
                {
                    SendWorldMessage("You're new. Let's add you to the adventure shall we?");
                    Globals.PassFile.AddUser(uname, unhashedpass);
                    Globals.CreateConfigFor(this);
                    SendWorldMessage("Your password will be '" + unhashedpass + "'. Please log in with your new credentials.");
                    _loggedIn = true;
                }
            }
            else
            {
                SendWorldMessage("The correct usage of login is: login username password");
            }
        }


        void SayCommand(List<string> message)
        {
            if (!_loggedIn) return;
            if (CurrentArea() == null) return;
            string strData = string.Join(" ", message.ToArray());
            if (!(strData.Length < 4)) // This is to avoid people from sending blank spaces to the console
            {
                CurrentArea().ChatToOccupants(_name, strData.Substring(3)); // We use the "Substring" function to avoid sending the string "say" to the screen.
            }
        }

        public void MoveToCommand(List<string> message)
        {
            if (!_loggedIn) return;
            if (message.Count >= 2)
            {
                string place = message[1].Trim();
                if (_location.LeadsTo(place))
                {
                    SendWorldMessage("You make your way to " + _location.Places(place).Name + ".");
                    _location.Places(place).MoveHere(this);
                    SendWorldMessage(_location.Description);
                }
                else
                {
                    SendWorldMessage("You seem to have a hard time getting there.");
                }
            }
            else
            {
                SendWorldMessage("The correct usage of moveto is: moveto <placeid>");
            }
        }


        //Ablities should all be public (so they can be accessed by the methods that give abilities)
        public void LookAtCommand(List<string> message)
        {
            if (!_loggedIn) return;
            if (message.Count >= 2)
            {
                string obj = message[1].Trim();
                if (CurrentArea().ContainsItem(obj))
                {
                    SendWorldMessage(CurrentArea().GetItem(obj).Description);
                    CurrentArea().GetItem(obj).OnLook(this);
                }
                else if (_inventory.ContainsKey(obj))
                {
                    SendWorldMessage(_inventory[obj].Description);
                    _inventory[obj].OnLook(this);
                }
                else
                {
                    SendWorldMessage("You cannot spot that item.");
                }
            }
            else
            {
                SendWorldMessage("The correct usage of lookat is: lookat <itemid>");
            }
        }

        public void LookAroundCommand(List<string> message)
        {
            if (_loggedIn)
            {
                SendWorldMessage("You spot " + CurrentArea().ItemList);
            }
        }

        public void PickUpCommand(List<string> message)
        {
            if (_loggedIn)
            {
                if (message.Count >= 2)
                {
                    if (CurrentArea().ContainsItem(message[1]))
                    {
                        if (CurrentArea().GetItem(message[1]).Obtainable) 
                        {
                            if (CurrentArea().GetItem(message[1]) is StackableItem)
                            {
                                Console.WriteLine("Found StackableItem...");
                                if (_inventory.ContainsKey(message[1]))
                                {
                                    Console.WriteLine("He already has this item so we should just increase the amount...");
                                    ((StackableItem)_inventory[message[1]]).Amount += 1;
                                    ((StackableItem)CurrentArea().GetItem(message[1])).Amount -= 1;
                                }
                                else
                                {
                                    Console.WriteLine("First time picking it up. Just gonna give it to him.");
                                    AddItem(CurrentArea().GetItem(message[1]));
                                    ((StackableItem)CurrentArea().GetItem(message[1])).Amount -= 1;
                                }
                            }
                            else
                            {
                                _inventory.Add(CurrentArea().GetItem(message[1]).ID, CurrentArea().GetItem(message[1]));
                            }
                        }
                        if (!HasPickedUp(CurrentArea().GetItem(message[1])))
                        {
                            CurrentArea().GetItem(message[1]).OnFirstPickUp(this);
                        }
                        
                    }
                }
            }
        }

        public void InventoryCommand(List<string> message)
        {
            if (_loggedIn)
            {
                SendInventory();
            }
        }
    }
}
